package com.ruoyi.quality.service.impl;

import com.ruoyi.base.domain.Product;
import com.ruoyi.base.mapper.ProductMapper;
import com.ruoyi.quality.mapper.QualityInspectionMapper;
import com.ruoyi.quality.pojo.QualityInspection;
import com.ruoyi.quality.pojo.SalesVo;
import com.ruoyi.quality.service.QualityInspectionService;
import com.ruoyi.sale.domain.SaleOrder;
import com.ruoyi.sale.mapper.SaleOrderMapper;
import com.ruoyi.stock.domain.WarehouseIn;
import com.ruoyi.stock.domain.WarehouseInInfo;
import com.ruoyi.stock.domain.WarehouseOut;
import com.ruoyi.stock.domain.WarehouseOutInfo;
import com.ruoyi.stock.mapper.WarehouseInInfoMapper;
import com.ruoyi.stock.mapper.WarehouseOutInfoMapper;
import com.ruoyi.stock.mapper.WarehouseOutMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 质检Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-25
 */
@Service
public class QualityInspectionServiceImpl implements QualityInspectionService {

    @Resource
    private QualityInspectionMapper qualityInspectionMapper;

    @Resource
    private WarehouseInInfoMapper warehouseInInfoMapper;

    @Resource
    private ProductMapper productMapper;

    @Resource
    private WarehouseOutInfoMapper warehouseOutInfoMapper;

    @Resource
    private SaleOrderMapper saleOrderMapper;

    @Resource
    private  WarehouseOutMapper warehouseOutMapper;

    /**
     * 获取入库检列表
     */
    @Override
    public List<QualityInspection> selectWarehouseInList(QualityInspection qualityInspection) {
        return qualityInspectionMapper.selectWarehouseInList(qualityInspection);
    }

    /**
     * 获取出库检列表
     */
    @Override
    public List<QualityInspection> selectWarehouseOutList(QualityInspection qualityInspection) {
        return qualityInspectionMapper.selectWarehouseOutList(qualityInspection);
    }

    /**
     * 获取销售出货订单产品列表
     */
    @Override
    public List<SalesVo> salesList(SalesVo salesVo) {
        return qualityInspectionMapper.salesList(salesVo);
    }

    /**
     * 获取生产入库产品列表
     */
    @Override
    public List<SalesVo> salesWorkList(SalesVo salesVo) {
        return qualityInspectionMapper.salesWorkList(salesVo);
    }

    @Override
    public List<SalesVo> salesPurchaseList(SalesVo salesVo) {
        return qualityInspectionMapper.salesPurchaseList(salesVo);
    }

    /**
     * 添加质检记录
     */
    @Transactional
    @Override
    public int insertQualityInspection(QualityInspection qualityInspection) {
        System.out.println("qualityInspection = " + qualityInspection);
        qualityInspection.getList().forEach(q -> {
            q.setQualityInspectionTime(LocalDateTime.now());
            q.setUserId(qualityInspection.getUserId());
            System.out.println("warehouseIn.getGoodsType() = " + qualityInspection.getGoodsType());
            if (qualityInspection.getGoodsType() == 1){
                q.setQualityInspectionType(1);
                q.setWarehouseInId(qualityInspection.getWarehouseInId());

            } else if (qualityInspection.getGoodsType() == 2){
                q.setQualityInspectionType(2);
                q.setWarehouseInId(qualityInspection.getWarehouseInId());

                WarehouseInInfo warehouseInInfo = new WarehouseInInfo();
                warehouseInInfo.setWarehouseInId(Long.valueOf(q.getWarehouseInId()));
                List<WarehouseInInfo> warehouseInInfos = warehouseInInfoMapper.selectWarehouseInInfoList(warehouseInInfo);
                for (WarehouseInInfo inInfo : warehouseInInfos) {
                    Product product = productMapper.selectProductById(Long.valueOf(q.getSaleOutProductId()));
                    if (product.getProductNo().equals(inInfo.getGoodsNo())){
                        WarehouseInInfo info = new WarehouseInInfo();
                        info.setId(inInfo.getId());
                        info.setWarehouseInNumber(Long.valueOf(q.getAcceptance()));
                        warehouseInInfoMapper.updateWarehouseInInfo(info);
                    }
                }
            } else if (qualityInspection.getGoodsType() == 3){
                q.setQualityInspectionType(3);
                q.setWarehouseOutId(qualityInspection.getWarehouseOutId());

                WarehouseOut warehouseOut = warehouseOutMapper.selectWarehouseOutById(Long.valueOf(q.getWarehouseOutId()));
                SaleOrder saleOrder = new SaleOrder();
                saleOrder.setOrderNo(warehouseOut.getSourceNo());
                List<SaleOrder> saleOrders = saleOrderMapper.selectSaleOrderList(saleOrder);
                System.out.println("saleOrders.get(0) = " + saleOrders.get(0));
                saleOrder.setStatus(8L);
                saleOrder.setId( saleOrders.get(0).getId());
                saleOrder.setOrderNo(null);
                saleOrderMapper.updateSaleOrder(saleOrder);

                WarehouseOutInfo warehouseOutInfo = new WarehouseOutInfo();
                warehouseOutInfo.setWarehouseOutId(Long.valueOf(q.getWarehouseOutId()));
                List<WarehouseOutInfo> warehouseInInfos = warehouseOutInfoMapper.selectWarehouseOutInfoList(warehouseOutInfo);
                for (WarehouseOutInfo inInfo : warehouseInInfos) {
                    Product product = productMapper.selectProductById(Long.valueOf(q.getSaleOutProductId()));
                    if (product.getProductNo().equals(inInfo.getGoodsNo())){
                        WarehouseOutInfo info = new WarehouseOutInfo();
                        info.setId(inInfo.getId());
                        info.setWarehouseOutNumber(Long.valueOf(q.getAcceptance()));
                        warehouseOutInfoMapper.updateWarehouseOutInfo(info);
                    }
                }
            }
            qualityInspectionMapper.insertQualityInspection(q);
            /**
             * 先通过入库id获取入库详情集合
             * 再遍历入库详情的集合
             * 再根据质检表中的产品id，查询出产品编号
             * 再根据查出的编号，对比找到该产品详情的id
             * 再根据id填入该产品的入库数量
             */





        });


        return 1;
    }
}
